package org.geek4j.business.service;

import java.util.ArrayList;
import java.util.List;

import org.geek4j.business.repository.mybatis.domain.Role;
import org.geek4j.business.repository.mybatis.domain.User;
import org.geek4j.business.repository.mybatis.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author Geek4J
 * @description UserServiceT
 * 
 */
@Slf4j
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/spring-context.xml")
public class UserServiceT {

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private UserService userService;

	/**
	 * Tester
	 */
	@Test
	public void log() {
		log.error("This is a tester for UserService");
	}

	/**
	 * Tester for findById
	 */
	@Test
	public void findById() {
		User user = userMapper.findById(1L);
		log.error(user.getName());
	}

	/**
	 * Tester for findByIdWithRoles
	 */
	@Test
	public void findByIdWithRoles() {
		User user = userMapper.findByIdWithRoles(1L);
		log.error(user.getName());
		for (Role role : user.getRoles()) {
			log.error(role.getName());
		}
	}

	/**
	 * Tester for findOneById
	 */
	@Test
	public void save() {
		List<User> users = new ArrayList<User>();
		for (int i = 0; i < 7; i++) {
			User user = new User();
			user.setName("Geek4J" + i);
			users.add(user);
		}
		userService.save(users);
	}
}
